package com.hkts.naturalgas.alarminformation.controller;

import com.hkts.naturalgas.alarminformation.entity.NaturalgasGateStation;
import com.hkts.naturalgas.alarminformation.service.INaturalgasGateStationService;
import com.hkts.naturalgas.utils.CustomPage;
import com.hkts.naturalgas.utils.Result;
import com.hkts.naturalgas.utils.ResultUtil;
import com.hkts.naturalgas.utils.TestDataUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Stream;

/**
 * @ClassNameAlarminFormationController
 * @Description
 * @Author yck
 * @Date2020-09-11 11:33
 * @Version V1.0
 **/
@RestController
@RequestMapping("/instrumentdata/dataStatistics")
public class AlarminFormationController {
    @Autowired
    public INaturalgasGateStationService iNaturalgasGateStationService;
    @GetMapping("/totalSiteData")
    @ResponseBody
    public Result<Object> alarminList(String category, String dateRange) {
        Map<String, Object> map = new HashMap<String, Object>();
        NaturalgasGateStation naturalgasGateStation = new NaturalgasGateStation();
        List<NaturalgasGateStation> naturalgasGateStations = iNaturalgasGateStationService.selectList(naturalgasGateStation);
        SimpleDateFormat dd = new SimpleDateFormat("yyyy-MM-dd");
        float maxValue = 0;
        float minValue = 0;
        float totalValue = 0;
        if (category.equals("0")) {
            if (dateRange.equals("0")) {
                map.put("hoursArray", TestDataUtils.getListHours(dd.format(TestDataUtils.getYesterday())));
                List<Map<String, Object>> gateData = new ArrayList<Map<String, Object>>();
                //按小时查数据
                List<List<Float>> listArray = new ArrayList<List<Float>>();
                for (int i = 0; i < naturalgasGateStations.size(); i++) {
                    float totalGateValue = 0;
                    float minGateValue = 0;
                    Map<String, Object> mapData = new HashMap<String, Object>();
                    List listHourData = new ArrayList();
                    for (int j = 0; j < 24; j++) {
                        float hoursData = TestDataUtils.getHoursData();
                        listHourData.add(hoursData);
                        totalGateValue = totalGateValue + hoursData;
                    }
                    listArray.add(listHourData);
                    mapData.put("gateName", naturalgasGateStations.get(i).getGateName());
                    mapData.put("totalGateValue", totalGateValue);
                    gateData.add(mapData);
                }
                map.put("gateData", TestDataUtils.sortLiet(gateData));
                List<Float> result = new ArrayList<Float>();
                for (int i = 0; i < 24; i++) {
                    float tempValue = 0;
                    for (int j = 0; j < listArray.size(); j++) {
                        tempValue = tempValue + listArray.get(j).get(i);
                    }
                    if (maxValue < tempValue) {
                        maxValue = tempValue;
                    }
                    if (i == 0) {
                        minValue = tempValue;
                    }
                    if (minValue > tempValue) {
                        minValue = tempValue;
                    }
                    result.add(tempValue);
                    totalValue = totalValue + tempValue;
                }
                map.put("maxValue", maxValue);
                map.put("minValue", minValue);
                map.put("totalValue", totalValue);
                map.put("result", result);
            }
            //近7天
            else if (dateRange.equals("1")) {
                //时间集合
                map.put("hoursArray",TestDataUtils.getDateArray(7));
                List<Map<String, Object>> gateData = new ArrayList<Map<String, Object>>();
                //按小时查数据
                List<List<Float>> listArray = new ArrayList<List<Float>>();
                for (int i = 0; i < naturalgasGateStations.size(); i++) {
                    float totalGateValue = 0;
                    float minGateValue = 0;
                    Map<String, Object> mapData = new HashMap<String, Object>();
                    List listHourData = new ArrayList();
                    for (int j = 0; j < 7; j++) {
                        float hoursData = TestDataUtils.getHoursData() * 24;
                        listHourData.add(hoursData);
                        totalGateValue = totalGateValue + hoursData;
                    }
                    listArray.add(listHourData);
                    mapData.put("gateName", naturalgasGateStations.get(i).getGateName());
                    mapData.put("totalGateValue", totalGateValue);
                    gateData.add(mapData);
                }
                map.put("gateData", TestDataUtils.sortLiet(gateData));
                List<Float> result = new ArrayList<Float>();
                for (int i = 0; i < 7; i++) {
                    float tempValue = 0;
                    for (int j = 0; j < listArray.size(); j++) {
                        tempValue = tempValue + listArray.get(j).get(i);
                    }
                    if (maxValue < tempValue) {
                        maxValue = tempValue;
                    }
                    if (i == 0) {
                        minValue = tempValue;
                    }
                    if (minValue > tempValue) {
                        minValue = tempValue;
                    }
                    result.add(tempValue);
                    totalValue = totalValue + tempValue;
                }
                map.put("maxValue", maxValue);
                map.put("minValue", minValue);
                map.put("totalValue", totalValue);
                map.put("result", result);
            } else if (dateRange.equals("2")) {
                map.put("hoursArray", TestDataUtils.getDateArray(30));
                List<Map<String, Object>> gateData = new ArrayList<Map<String, Object>>();
                //按小时查数据
                List<List<Float>> listArray = new ArrayList<List<Float>>();
                for (int i = 0; i < naturalgasGateStations.size(); i++) {
                    float totalGateValue = 0;
                    float minGateValue = 0;
                    Map<String, Object> mapData = new HashMap<String, Object>();
                    List listHourData = new ArrayList();
                    for (int j = 0; j < 30; j++) {
                        float hoursData = TestDataUtils.getHoursData() * 24;
                        listHourData.add(hoursData);
                        totalGateValue = totalGateValue + hoursData;
                    }
                    listArray.add(listHourData);
                    mapData.put("gateName", naturalgasGateStations.get(i).getGateName());
                    mapData.put("totalGateValue", totalGateValue);
                    gateData.add(mapData);
                }
                map.put("gateData", TestDataUtils.sortLiet(gateData));
                List<Float> result = new ArrayList<Float>();
                for (int i = 0; i < 30; i++) {
                    float tempValue = 0;
                    for (int j = 0; j < listArray.size(); j++) {
                        tempValue = tempValue + listArray.get(j).get(i);
                    }
                    if (maxValue < tempValue) {
                        maxValue = tempValue;
                    }
                    if (i == 0) {
                        minValue = tempValue;
                    }
                    if (minValue > tempValue) {
                        minValue = tempValue;
                    }
                    result.add(tempValue);
                    totalValue = totalValue + tempValue;
                }
                map.put("maxValue", maxValue);
                map.put("minValue", minValue);
                map.put("totalValue", totalValue);
                map.put("result", result);
            }
        } else if (category.equals("1")) {
            if (dateRange.equals("0")) {
                List<String> listHours = new ArrayList<>();
                listHours.add(dd.format(TestDataUtils.getYesterday()));
                map.put("hoursArray", listHours);
                List<Map<String, Object>> gateData = new ArrayList<Map<String, Object>>();
                //按小时查数据
                List<List<Float>> listArray = new ArrayList<List<Float>>();
                for (int i = 0; i < naturalgasGateStations.size(); i++) {
                    float totalGateValue = 0;
                    float minGateValue = 0;
                    Map<String, Object> mapData = new HashMap<String, Object>();
                    List listHourData = new ArrayList();
                    for (int j = 0; j < 24; j++) {
                        float hoursData = TestDataUtils.getHoursData();
                        listHourData.add(hoursData);
                        totalGateValue = totalGateValue + hoursData;
                    }
                    listArray.add(listHourData);
                    mapData.put("gateName", naturalgasGateStations.get(i).getGateName());
                    mapData.put("totalGateValue", totalGateValue);
                    gateData.add(mapData);
                }
                map.put("gateData", TestDataUtils.sortLiet(gateData));
                List<Float> result = new ArrayList<Float>();
                for (int i = 0; i < 24; i++) {
                    float tempValue = 0;
                    for (int j = 0; j < listArray.size(); j++) {
                        tempValue = tempValue + listArray.get(j).get(i);
                    }
                    if (maxValue < tempValue) {
                        maxValue = tempValue;
                    }
                    if (i == 0) {
                        minValue = tempValue;
                    }
                    if (minValue > tempValue) {
                        minValue = tempValue;
                    }
                    totalValue = totalValue + tempValue;
                }
                result.add(totalValue);
                map.put("maxValue", maxValue);
                map.put("minValue", minValue);
                map.put("totalValue", totalValue);
                map.put("result", result);
            } else if (dateRange.equals("1")) {
                map.put("hoursArray", TestDataUtils.getDateArray(7));
                List<Map<String, Object>> gateData = new ArrayList<Map<String, Object>>();
                //按小时查数据
                List<List<Float>> listArray = new ArrayList<List<Float>>();
                for (int i = 0; i < naturalgasGateStations.size(); i++) {
                    float totalGateValue = 0;
                    float minGateValue = 0;
                    Map<String, Object> mapData = new HashMap<String, Object>();
                    List listHourData = new ArrayList();
                    for (int j = 0; j < 7; j++) {
                        float hoursData = TestDataUtils.getHoursData() * 24;
                        listHourData.add(hoursData);
                        totalGateValue = totalGateValue + hoursData;
                    }
                    listArray.add(listHourData);
                    mapData.put("gateName", naturalgasGateStations.get(i).getGateName());
                    mapData.put("totalGateValue", totalGateValue);
                    gateData.add(mapData);
                }
                map.put("gateData", TestDataUtils.sortLiet(gateData));
                List<Float> result = new ArrayList<Float>();
                for (int i = 0; i < 7; i++) {
                    float tempValue = 0;
                    for (int j = 0; j < listArray.size(); j++) {
                        tempValue = tempValue + listArray.get(j).get(i);
                    }
                    if (maxValue < tempValue) {
                        maxValue = tempValue;
                    }
                    if (i == 0) {
                        minValue = tempValue;
                    }
                    if (minValue > tempValue) {
                        minValue = tempValue;
                    }
                    result.add(tempValue);
                    totalValue = totalValue + tempValue;
                }
                map.put("maxValue", maxValue);
                map.put("minValue", minValue);
                map.put("totalValue", totalValue);
                map.put("result", result);
            } else if (dateRange.equals("2")) {
                map.put("hoursArray", TestDataUtils.getDateArray(30));
                List<Map<String, Object>> gateData = new ArrayList<Map<String, Object>>();
                //按小时查数据
                List<List<Float>> listArray = new ArrayList<List<Float>>();
                for (int i = 0; i < naturalgasGateStations.size(); i++) {
                    float totalGateValue = 0;
                    float minGateValue = 0;
                    Map<String, Object> mapData = new HashMap<String, Object>();
                    List listHourData = new ArrayList();
                    for (int j = 0; j < 30; j++) {
                        float hoursData = TestDataUtils.getHoursData() * 24;
                        listHourData.add(hoursData);
                        totalGateValue = totalGateValue + hoursData;
                    }
                    listArray.add(listHourData);
                    mapData.put("gateName", naturalgasGateStations.get(i).getGateName());
                    mapData.put("totalGateValue", totalGateValue);
                    gateData.add(mapData);
                }
                map.put("gateData", TestDataUtils.sortLiet(gateData));
                List<Float> result = new ArrayList<Float>();
                for (int i = 0; i < 30; i++) {
                    float tempValue = 0;
                    for (int j = 0; j < listArray.size(); j++) {
                        tempValue = tempValue + listArray.get(j).get(i);
                    }
                    if (maxValue < tempValue) {
                        maxValue = tempValue;
                    }
                    if (i == 0) {
                        minValue = tempValue;
                    }
                    if (minValue > tempValue) {
                        minValue = tempValue;
                    }
                    result.add(tempValue);
                    totalValue = totalValue + tempValue;
                }
                map.put("maxValue", maxValue);
                map.put("minValue", minValue);
                map.put("totalValue", totalValue);
                map.put("result", result);
            }
        } else if (category.equals("2")) {
            if (dateRange.equals("0")) {
                List<String> listHours = new ArrayList<>();
                listHours.add(dd.format(TestDataUtils.getYesterday()));
                map.put("hoursArray", listHours);
                List<Map<String, Object>> gateData = new ArrayList<Map<String, Object>>();
                //按小时查数据
                List<List<Float>> listArray = new ArrayList<List<Float>>();
                for (int i = 0; i < naturalgasGateStations.size(); i++) {
                    float totalGateValue = 0;
                    float minGateValue = 0;
                    Map<String, Object> mapData = new HashMap<String, Object>();
                    List listHourData = new ArrayList();
                    for (int j = 0; j < 24; j++) {
                        float hoursData = TestDataUtils.getHoursData();
                        listHourData.add(hoursData);
                        totalGateValue = totalGateValue + hoursData;
                    }
                    listArray.add(listHourData);
                    mapData.put("gateName", naturalgasGateStations.get(i).getGateName());
                    mapData.put("totalGateValue", totalGateValue);
                    gateData.add(mapData);
                }
                map.put("gateData", gateData);
                List<Float> result = new ArrayList<Float>();
                for (int i = 0; i < 24; i++) {
                    float tempValue = 0;
                    for (int j = 0; j < listArray.size(); j++) {
                        tempValue = tempValue + listArray.get(j).get(i);
                    }
                    if (maxValue < tempValue) {
                        maxValue = tempValue;
                    }
                    if (i == 0) {
                        minValue = tempValue;
                    }
                    if (minValue > tempValue) {
                        minValue = tempValue;
                    }
                    totalValue = totalValue + tempValue;
                }
                result.add(totalValue / 24);
                map.put("maxValue", maxValue);
                map.put("minValue", minValue);
                map.put("totalValue", totalValue);
                map.put("result", result);
            } else if (dateRange.equals("1")) {
                map.put("hoursArray", TestDataUtils.getDateArray(7));
                List<Map<String, Object>> gateData = new ArrayList<Map<String, Object>>();
                //按小时查数据
                List<List<Float>> listArray = new ArrayList<List<Float>>();
                for (int i = 0; i < naturalgasGateStations.size(); i++) {
                    float totalGateValue = 0;
                    float minGateValue = 0;
                    Map<String, Object> mapData = new HashMap<String, Object>();
                    List listHourData = new ArrayList();
                    for (int j = 0; j < 7; j++) {
                        float hoursData = TestDataUtils.getHoursData();
                        listHourData.add(hoursData);
                        totalGateValue = totalGateValue + hoursData;
                    }
                    listArray.add(listHourData);
                    mapData.put("gateName", naturalgasGateStations.get(i).getGateName());
                    mapData.put("totalGateValue", totalGateValue);
                    gateData.add(mapData);
                }
                map.put("gateData", TestDataUtils.sortLiet(gateData));
                List<Float> result = new ArrayList<Float>();
                for (int i = 0; i < 7; i++) {
                    float tempValue = 0;
                    for (int j = 0; j < listArray.size(); j++) {
                        tempValue = tempValue + listArray.get(j).get(i);
                    }
                    if (maxValue < tempValue) {
                        maxValue = tempValue;
                    }
                    if (i == 0) {
                        minValue = tempValue;
                    }
                    if (minValue > tempValue) {
                        minValue = tempValue;
                    }
                    result.add(tempValue);
                    totalValue = totalValue + tempValue;
                }
                map.put("maxValue", maxValue);
                map.put("minValue", minValue);
                map.put("totalValue", totalValue);
                map.put("result", result);
            }
            if (dateRange.equals("2")) {
                map.put("hoursArray", TestDataUtils.getDateArray(30));
                List<Map<String, Object>> gateData = new ArrayList<Map<String, Object>>();
                //按小时查数据
                List<List<Float>> listArray = new ArrayList<List<Float>>();
                for (int i = 0; i < naturalgasGateStations.size(); i++) {
                    float totalGateValue = 0;
                    float minGateValue = 0;
                    Map<String, Object> mapData = new HashMap<String, Object>();
                    List listHourData = new ArrayList();
                    for (int j = 0; j < 30; j++) {
                        float hoursData = TestDataUtils.getHoursData();
                        listHourData.add(hoursData);
                        totalGateValue = totalGateValue + hoursData;
                    }
                    listArray.add(listHourData);
                    mapData.put("gateName", naturalgasGateStations.get(i).getGateName());
                    mapData.put("totalGateValue", totalGateValue);
                    gateData.add(mapData);
                }
                map.put("gateData", TestDataUtils.sortLiet(gateData));
                List<Float> result = new ArrayList<Float>();
                for (int i = 0; i < 30; i++) {
                    float tempValue = 0;
                    for (int j = 0; j < listArray.size(); j++) {
                        tempValue = tempValue + listArray.get(j).get(i);
                    }
                    if (maxValue < tempValue) {
                        maxValue = tempValue;
                    }
                    if (i == 0) {
                        minValue = tempValue;
                    }
                    if (minValue > tempValue) {
                        minValue = tempValue;
                    }
                    result.add(tempValue);
                    totalValue = totalValue + tempValue;
                }
                map.put("maxValue", maxValue);
                map.put("minValue", minValue);
                map.put("totalValue", totalValue);
                map.put("result", result);
            }
        }
        return ResultUtil.data(map);
    }

}
